#import "notMacPref.h"

@implementation notMacPref

- (BOOL)validateAccount:(notMac *)checkAccount
{
    [progressIndicator startAnimation:self];

    if ([checkAccount validate])
    {
	NSDictionary *info = [checkAccount storageInfo];

	[storageAccount setStringValue:[checkAccount username]];
	[storageCapacity setStringValue:[NSString stringWithFormat:@"%d MB", [[info valueForKey:@"quota"] intValue]/1024/1024]];
	[storageCapacityUsed setStringValue:[NSString stringWithFormat:@"%d MB", [[info valueForKey:@"quotaUsed"] intValue]/1024/1024]];
	[storageIndicator setMaxValue:[[info valueForKey:@"quota"] intValue]];
	[storageIndicator setIntValue:[[info valueForKey:@"quotaUsed"] intValue]];
	[publicAccess selectCellAtRow:0 column:[[info valueForKey:@"publicAccess"] isEqualTo:@"read-write"] ? PA_READWRITE : PA_READONLY];
	[publicPasswordProtected setState:[[info valueForKey:@"publicPasswordSet"] intValue] ? NSOnState : NSOffState];

	[statusDisplay setStringValue:@"Success"];

	account = [checkAccount retain];
    }
    else
    {
	[statusDisplay setStringValue:@"Failure"];

        [account release];
	account = nil;
    }

    [progressIndicator stopAnimation:self];

    return account ? YES : NO;
}

- (void)addServiceToMenu:(NSString *)url selected:(BOOL)doSelect
{
    int index;
	
    if ((index = [menu indexOfItemWithTitle:url]) < 0)
    {
	index = [menu numberOfItems] - 1;

        [menu insertItemWithTitle:url action:nil keyEquivalent:@"" atIndex:index];
    }

    if (doSelect)
	[serviceField selectItemAtIndex:index];
}

- (void)awakeFromNib
{
    menu = [[NSMenu alloc] initWithTitle:@"Service URLS:"];
    [[menu insertItemWithTitle:@"Add new..." action:@selector(enterServiceURL:) keyEquivalent:@"n" atIndex:0] setTarget:self];
    [self addServiceToMenu:NOTMAC_URL selected:YES];

    [serviceField setMenu:menu];

    if ([self validateAccount:[notMac notMacFromEnvironment]])
    {
	[self addServiceToMenu:[account serviceURL] selected:YES];
        [usernameField setStringValue:[account username]];
        [passwordField setStringValue:[account password]];
    }

    if (browser = [[NSNetServiceBrowser alloc] init])
    {
        [browser setDelegate:self];
        [browser searchForServicesOfType:MDNS_TYPE inDomain:@""];
    }

    if (browserSSL = [[NSNetServiceBrowser alloc] init])
    {
        [browserSSL setDelegate:self];
        [browserSSL searchForServicesOfType:MDNS_SSL_TYPE inDomain:@""];
    }
}

- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
    if (!account)
        return NO;

    return YES;
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
    [aNetService setDelegate:self];
    [aNetService resolve];
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
    int i = 0;

    for (i = 0; i < [[sender addresses] count]; i++)
    {
        struct sockaddr_in *socketAddress = (struct sockaddr_in *)[[[sender addresses] objectAtIndex:i] bytes];
        NSString *serviceURL = nil;
	int port = ntohs(socketAddress->sin_port);
	
	if ([[sender type] isEqualToString:MDNS_SSL_TYPE])
	    serviceURL = [NSString stringWithFormat:@"https://%@%@/", [sender hostName], port == 443 ? @"" : [NSString stringWithFormat:@":%d", port]];
	else
	    serviceURL = [NSString stringWithFormat:@"http://%@%@/", [sender hostName], port == 80 ? @"" : [NSString stringWithFormat:@":%d", port]];
	
	[self addServiceToMenu:serviceURL selected:NO];
    }
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
    NSString *username = [usernameField stringValue];
    NSString *password = [passwordField stringValue];
    NSString *serviceURL = [serviceField titleOfSelectedItem];

    if ([self validateAccount:[notMac notMacWithUsername:username password:password serviceURL:serviceURL]])
	[account saveToEnvironment];
    else
	[notMac clearEnvironment];
}

- (void)enterServiceURL:(id)sender
{
    [NSApp beginSheet:newServiceWindow modalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:NULL contextInfo:nil];
}

- (void)addServiceURL:(id)sender
{
    [self addServiceToMenu:[newServiceURL stringValue] selected:YES];
    [self closeServiceWindow:sender];
}
	
- (void)closeServiceWindow:(id)sender
{
    [newServiceURL setStringValue:@""];	
    [newServiceWindow orderOut:nil];
    [NSApp endSheet:newServiceWindow];
}

- (IBAction)setPublicFolderPassword:(id)sender
{
    0;
}

@end
